package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.card.EntityPointConfig;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.restaurant.RestaurantAddRequest;
import com.ocom.common.vo.payconfig.kitchenprinter.PointConfigOnPointInfoDto;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoPointConfig;
import com.ocom.payconfig.service.PointConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class PointConfigServiceImpl extends ServiceImpl<IDaoPointConfig, EntityPointConfig> implements PointConfigService {

    @Autowired
    private IDaoPointConfig iDaoPointConfig;

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityPointConfig> restaurantList(Long comId, String strPoints, String pointName, int size, int page) {

        List<String> strPointList = new ArrayList<>();
        if (StringUtils.isNotBlank(strPoints)) {
            strPointList = Arrays.stream(strPoints.split(",")).collect(Collectors.toList());
        }
        PageHelper.startPage(page, size);

        List<EntityPointConfig> entityPointConfigs = iDaoPointConfig.restaurantList(comId, strPointList, pointName);

        return entityPointConfigs;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    public boolean removeRestaurant(Long comId, Long pointId) {
        QueryWrapper<EntityPointConfig> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("point_id", pointId);
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    public EntityPointConfig newRestaurant(Long comId, Long pointId) {
        EntityPointConfig entity = new EntityPointConfig();
        entity.setComId(comId);
        entity.setPointId(pointId);
        this.save(entity);
        return entity;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    public EntityPointConfig newRestaurant(Long comId, RestaurantAddRequest request) {
        EntityPointConfig entity = new EntityPointConfig();
        entity.setComId(comId);
        entity.setPointId(request.getPointId());
        entity.setLimitType(request.getLimitType());
        this.save(entity);
        return entity;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    public boolean setRestaurantLogo(Long comId, Long pointId, String logoUrl) {
        UpdateWrapper<EntityPointConfig> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", comId);
        updateWrapper.eq("point_id", pointId);

        updateWrapper.set("logo_url", logoUrl);

        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    public boolean restaurantQrCode(Long comId, Long pointId, String appid, String qrCodeUrl) {

        UpdateWrapper<EntityPointConfig> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", comId);
        updateWrapper.eq("point_id", pointId);

        updateWrapper.set("gzh_appid", appid);
        updateWrapper.set("qr_code_url", qrCodeUrl);
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    public boolean saveRestaurantInfo(Long comId, Long pointId, String address, String remark) {
        UpdateWrapper<EntityPointConfig> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", comId);
        updateWrapper.eq("point_id", pointId);

        updateWrapper.set("address", address);
        updateWrapper.set("remark", remark);

        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    public boolean saveRestaurantInfo(Long comId, Long pointId, String address, String remark, Integer limitType) {
        UpdateWrapper<EntityPointConfig> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", comId);
        updateWrapper.eq("point_id", pointId);

        updateWrapper.set("address", address);
        updateWrapper.set("remark", remark);
        updateWrapper.set("limit_type", limitType);

        return this.update(updateWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<PointConfigOnPointInfoDto> findPointConfigOnPointInfoByPointId(Long comId, Long pointId) {

        return iDaoPointConfig.findPointConfigOnPointInfoByPointId(comId, pointId);
    }

}
